import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import 'package:toys_flutter/components/RefreshableListView.dart';
import 'package:toys_flutter/pages/LoginPage.dart';

class UserSetting extends StatefulWidget {
  const UserSetting({super.key});

  @override
  _UserSettingState createState() => _UserSettingState();
}

class _UserSettingState extends State<UserSetting> {
  // 模拟数据刷新
  Future<void> _refreshData() async {
    // 模拟网络请求或数据处理
    await Future.delayed(const Duration(seconds: 2));
    // 这里可以添加你需要的刷新逻辑
    print('数据已刷新');
    TDToast.showIconText('数据已刷新',
        icon: TDIcons.check_circle,
        direction: IconTextDirection.vertical,
        context: context);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('个人设置'),
      ),
      body: RefreshableListView(
        onRefresh: _refreshData, // 绑定刷新逻辑
        children: [
          // 用户信息区域
          const UserInfoSection(),
          const TDDivider(),
          const TDCell(arrow: true, title: '关于我们', note: '1.0.0'),
          const TDCell(arrow: true, title: '隐私政策', note: '查看隐私政策'),
          const TDCell(arrow: true, title: '使用条款', note: '查看使用条款'),
          const SizedBox(height: 20),
          // 退出登录按钮
          Center(
            child: TextButton(
              style: TextButton.styleFrom(
                foregroundColor: Colors.white,
                backgroundColor: Colors.red, // 按钮背景颜色
              ),
              onPressed: () {
                TDToast.showText('退出登录', context: context);
                // 延时 1秒钟 再执行
                Future.delayed(const Duration(seconds: 1), () {
                  //  跳转到登录页面
                  Get.to(const LoginPage());
                });
              },
              child: const Text('退出登录'),
            ),
          ),
        ],
      ),
    );
  }
}

// 用户信息区域
class UserInfoSection extends StatelessWidget {
  const UserInfoSection({super.key});

  @override
  Widget build(BuildContext context) {
    return const Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Padding(
          padding: EdgeInsets.all(15),
          child: TDAvatar(
            size: TDAvatarSize.medium,
            type: TDAvatarType.normal,
            defaultUrl: 'images/avatar.png', // 修改为绝对路径
          ),
        ),
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            Text('用户名', style: TextStyle(fontSize: 16)),
            Text('用户ID', style: TextStyle(fontSize: 12, color: Colors.black38)),
          ],
        )
      ],
    );
  }
}

// 列表项组件
class ListTileItem extends StatelessWidget {
  final String title;
  final String subtitle;
  final VoidCallback onTap;

  const ListTileItem({
    required this.title,
    required this.subtitle,
    required this.onTap,
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: onTap,
      child: Container(
        height: 60,
        margin: const EdgeInsets.symmetric(horizontal: 15),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(title),
                Row(
                  children: [
                    Text(
                      subtitle,
                      style: const TextStyle(color: Colors.black38),
                    ),
                    const SizedBox(width: 5),
                    const Icon(
                      Icons.arrow_forward_ios,
                      size: 15,
                    ),
                  ],
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

void main() {
  runApp(const MaterialApp(
    home: UserSetting(),
  ));
}
